package com.cugmarket.controller.product.user;

import com.cugmarket.dto.Result;
import com.cugmarket.entity.ProductInfo;
import com.cugmarket.entity.ProductVoucher;
import com.cugmarket.service.ProductVoucherService;
import com.cugmarket.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.cugmarket.constant.MessageConstant.*;
import static com.cugmarket.constant.MessageConstant.DELETE_ERROR;

@Slf4j
@RestController
@RequestMapping("/productVoucher")
public class ProductVoucherController {
    @Resource
    private ProductVoucherService productVoucherService;

    @PostMapping("/add")
    public Result add(@RequestBody ProductVoucher productVoucher) {
        productVoucherService.add(productVoucher);
        return Result.success(productVoucher.getId());
    }

    @DeleteMapping("/delete")
    public Result del(@RequestParam("id") String id) {
        if (id == null) {
            return Result.error(DELETE_ERROR);
        }
        if (!productVoucherService.removeById(id)) {
            return Result.error(DELETE_ERROR);
        }
        return Result.success(DELETE_SUCCESS);
    }

    @PutMapping("/update")
    public Result update(@RequestBody ProductVoucher productVoucher) {
        boolean flag = productVoucherService.updateById(productVoucher);
        if (!flag) {
            return Result.error(UPDATE_ERROR);
        }
        return Result.success(UPDATE_SUCCESS);
    }

    @GetMapping("/id")
    public Result getById(@RequestParam("id") String id) {
        ProductVoucher productVoucher = productVoucherService.getById(id);
        if (productVoucher == null) {
            return Result.error(SEARCH_ERROR);
        }
        return Result.success(productVoucher);
    }
}
